const {
  toAttend,
  getAttence,
  attendMenology
} = require('../../api/geData.js')
const {
  formatTime,
  formatNumber
} = require('../../utils/util.js');
const {
  globalData
} = getApp()
Component({
  properties: {
    fromSign: null,
    courseD: null,
    titleCho:null
  },
  data: {
    coverMonthList: [],
    countData: [{
      month: '2018-07',
      days: '12', // 出勤天数
      lateDays: 2, // 迟到天数
      truantDays: 3, // 旷课
      showTick: true
    }],
    nowMonth: null,
    courseData: null,
    calendarData: null,
    current: null,
    showMonth: false,
    formerIndex: 0
  },
  attached() {
  },
  methods: {
    // 月份蒙版层函数
    chooseMonth(e) {
      let idx = e.currentTarget.dataset.index

      let coverMonthList = this.data.coverMonthList,
        month = e.currentTarget.dataset.month,
        courseData = this.data.courseData,
        that = this
  

      coverMonthList.map((item, index) => {
        item.showTick = (idx == index) && true || false // 点击哪个课程 打勾显示哪个
      })

      this.setData({
        coverMonthList: coverMonthList,
        current: coverMonthList[idx],
        nowMonth: month.split('-')[1],
        showMonth: false,
        formerIndex: idx
      })
      attendMenology(wx.getStorageSync('userInfo').cluesOrStuId, courseData.formalClassId, coverMonthList[idx].month.split('-').join('')).then(res => {
        that.setData({
          countData: res.menologyStatistics,
          calendarData: res.attendMenology
        })
      }).catch(err => {
        console.log(err)
      })
    },

    // 显示月份蒙版层函数
    showMonthFn() {
      if (this.data.coverMonthList.length * 1 <= 1) return
      this.setData({
        showMonth: true
      })
    },
    closeCover(){
      this.setData({
        showMonth: false
      })
    },

    getCountData(courseData) {
      this.setData({
        courseData: courseData
      })
      let monthBegin = [new Date(courseData.classBegin*1).getFullYear(), new Date(courseData.classBegin*1).getMonth() + 1]
      let monthEnd = [new Date(courseData.classEnd*1).getFullYear(), new Date(courseData.classEnd*1).getMonth() + 1]
      this.formatMonth(monthBegin, monthEnd)
    },

    // 格式化月份
    formatMonth(monthBegin, monthEnd) {
      let month = [],
        starMonth = monthBegin[1],
        that = this
      // console.log('666')

      if (monthBegin[0] == monthEnd[0]) {
        for (let i = starMonth; i <= monthEnd[1]; i++) {
          let data = {}
          data.month = monthBegin[0] + '-' + formatNumber(i)
          data.showTick = (i == starMonth) && true || false
          month.push(data)
        }
      } else {
        for (let i = starMonth; i <= 12; i++) {
          let data = {}
          data.month = monthBegin[0] + '-' + formatNumber(i)
          data.showTick = (i == starMonth) && true || false
          month.push(data)
          if (i == 12) {
            for (let j = 1; j <= monthEnd[1]; j++) {
              let data = {}
              
              data.month = monthEnd[0] + '-' + formatNumber(j)
              data.showTick = (j == starMonth) && true || false
              month.push(data)
            }
          }
        }
      }
      if (!this.properties.titleCho){
      wx.showLoading({
        title: '加载中...',
        })
      }
      attendMenology(wx.getStorageSync('userInfo').cluesOrStuId, this.data.courseData.formalClassId, month[0].month.split('-').join('')).then(res => {
        wx.hideLoading()
        wx.setStorageSync('claState', res.claState)
        that.setData({
          countData: res.menologyStatistics,
          calendarData: res.attendMenology
        })
      }).catch(err => {
        console.log(err)
      })

      // console.log(month)
      this.setData({
        coverMonthList: month,
        nowMonth: month[0].month.split('-')[1],
        current: month[0]
      })
    },

    // 跳转到月历
    goCalendar() {
      // console.log(this.data.courseData)
      let calendarAllData = {
        coverMonthList: this.data.coverMonthList,
        courseData: this.data.courseData,
        calendarData: this.data.calendarData
      }
      
      if (!this.data.courseData.formalClassId) return
      wx.setStorageSync('calendarAllData', calendarAllData)
      wx.navigateTo({
        url: `/pages/calendar/calendar`
      })
    }
  }

})